<?php

namespace App\Models\Mini;

use App\Models\User;
use Illuminate\Database\Eloquent\Model;

class FleaMsgModel extends Model
{
    protected $table = 'flea_msg';

    public $timestamps = false;

    /**
     * @Notes 创建时间的时间格式
     * @param $value
     * @return false|string
     * @author ypp
     * @date 2021/3/8
     * @time 10:47
     */
    public function getCreatedAtAttribute($value)
    {
        return date('Y-m-d H:i:s', $value);
    }

    public function allChildren() {
        return $this->hasMany(FleaMsgModel::class, 'pid', 'id')->with('user_info')->select();
    }

    public function children()
    {
        return $this->allChildren()->with('children');
    }

    /**
     * @Notes 关联用户模型
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     * @author ypp
     * @date 2021/3/16
     * @time 14:09
     */
    public function user_info()
    {
        return $this->hasOne(User::class, 'id', 'user_id')->selectRaw('id,username,avatar');
    }

}
